/**
 * 数组去重
 * @param {Array} arr
 * @returns {Array}
 */
export function uniqueArr(arr) {
	return Array.from(new Set(arr))
}
/**
 * 数组对象去重
 * @param {*} arr 去重数组
 * @param {*} uniId 根据哪个字段去重
 */
export const uniqueFunc = (arr, uniId) => {
	const res = new Map()
	return arr.filter(item => !res.has(item[uniId]) && res.set(item[uniId], 1))
}

export function parseDate(time, cFormat) {
	if (!time) {
	  return ''
	}
	const format = cFormat || '{yyyy}-{mm}-{dd} {hh}:{ii}:{ss}'
	let date
	if (typeof time === 'object') {
	  date = time
	} else {
	  if (parseInt(time) + ''.length === 10) {
		time = parseInt(time) * 1000
	  }
	  date = new Date(time)
	}
	const formatObj = {
	  y: date.getFullYear(),
	  m: date.getMonth() + 1,
	  d: date.getDate(),
	  h: date.getHours(),
	  i: date.getMinutes(),
	  s: date.getSeconds(),
	  a: date.getDay()
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
	  let value = formatObj[key]
	  if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
	  if (result.length > 0 && value < 10) {
		value = '0' + value
	  }
	  return value || 0
	})
	return time_str
  }
  
  /**
   * 获取当前日期时间
   */
  export const getNowDate = dateType => {
  	var now = new Date()
  	var year = now.getFullYear()
  	var month = now.getMonth() + 1
  	month = month < 10 ? '0' + month : month
  	var day = now.getDate()
  	day = day < 10 ? '0' + day : day
  	var hour = now.getHours()
  	var min = now.getMinutes()
  	min = min < 10 ? '0' + min : min
  	var sec = now.getSeconds()
  	sec = sec < 10 ? '0' + sec : sec
  	if (dateType == 'YYYY-MM-DD hh:mm:ss') {
  		return year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec
  	} else if (dateType == 'YYYY-MM-DD hh:mm') {
  		return year + '-' + month + '-' + day + ' ' + hour + ':' + min
  	} else if (dateType == 'YYYY-MM-DD hh') {
  		return year + '-' + month + '-' + day + ' ' + hour
  	} else if (dateType == 'YYYY-MM-DD') {
  		return year + '-' + month + '-' + day
  	} else if (dateType == 'YYYY-MM') {
  		return year + '-' + month
  	} else if (dateType == 'YYYY') {
  		return year
  	}
  }
  
  /**
   * 格式化金额
   * @param value
   * @returns {string|boolean}
   */
  export function formatMoney(value) {
  	// console.log(value, 'value')
  	if (!value) {
  		return '-'
  	}
  	//强制保留两位小数
  	let f = parseFloat(value)
  	if (isNaN(f)) return false
  	f = Math.round(value * 100) / 100
  	let s = f.toString()
  	let rs = s.indexOf('.')
  	if (rs < 0) {
  		rs = s.length
  		s += '.'
  	}
  	while (s.length < rs + 1 + 2) {
  		s += '0'
  	}
  	//每三位用一个逗号隔开
  	let leftNum = s.split('.')[0]
  	let rightNum = '.' + s.split('.')[1]
  	let result
  	//定义数组记录截取后的价格
  	let resultArray = new Array()
  	if (leftNum.length > 3) {
  		let i = true
  		while (i) {
  			resultArray.push(leftNum.slice(-3))
  			leftNum = leftNum.slice(0, leftNum.length - 3)
  			if (leftNum.length < 4) {
  				i = false
  			}
  		}
  		//由于从后向前截取，所以从最后一个开始遍历并存到一个新的数组，顺序调换
  		let sortArray = new Array()
  		for (let i = resultArray.length - 1; i >= 0; i--) {
  			sortArray.push(resultArray[i])
  		}
  		result = leftNum + ',' + sortArray.join(',') + rightNum
  	} else {
  		result = s
  	}
  	return result
  }